var totalPage
var pageNum
var spanPre
var spanNext
var spanFirst
var spanLast
var numberRowsInTable
var pageSize
var page
var theUL
$(function (){
    theUL = document.getElementById("pageListTableBody");
    totalPage = document.getElementById("spanTotalPage");
    pageNum = document.getElementById("spanPageNum"); //获取当前页<span>
    spanPre = document.getElementById("spanPre"); //获取上一页<span>
    spanNext = document.getElementById("spanNext"); //获取下一页<span>
    spanFirst = document.getElementById("spanFirst"); //获取第一页<span>
    spanLast = document.getElementById("spanLast"); //获取最后一页<span>
    numberRowsInTable = theUL.getElementsByTagName("tr").length; //记录总条数
    pageSize = 10; //每页显示的记录条数
    page = 1; //当前页，默认第一页
    hide();

})
//下一页
next=function (){
    hideTable();
    currentRow = pageSize * page;
    maxRow = currentRow + pageSize;
    if ( maxRow > numberRowsInTable ) maxRow = numberRowsInTable;
    for ( var i = currentRow; i< maxRow; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    page++;
    if ( maxRow == numberRowsInTable ) {
        nextText();
        lastText();
    }
    showPage();
    preLink();
    firstLink();
}

//上一页
pre=function (){
    hideTable();
    page--;
    currentRow = pageSize * page;
    maxRow = currentRow - pageSize;
    if ( currentRow > numberRowsInTable ) currentRow = numberRowsInTable;
    for ( var i = maxRow; i< currentRow; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    if ( maxRow == 0 ){
        preText();
        firstText();
    }
    showPage();
    nextLink();
    lastLink();
}

//第一页
first=function (){
    hideTable();
    page = 1;
    for ( var i = 0; i<pageSize; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    firstText();
    preText();
    nextLink();
    lastLink();
}

//最后一页
last=function (){
    hideTable();
    page = pageCount();
    currentRow = pageSize * (page - 1);
    for ( var i = currentRow; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = '';
    }
    showPage();
    preLink();
    nextText();
    firstLink();
    lastText();
}

hideTable =function (){
    for ( var i = 0; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
}

showPage=function (){
    pageNum.innerHTML = page;
}

//总共页数
pageCount =function (){
    return Math.ceil(numberRowsInTable/pageSize);
}
//显示链接
preLink =function (){
    spanPre.innerHTML = "<a href='javascript:pre();'>上一页</a>";
}
preText =function (){
    spanPre.innerHTML = "上一页";
}
nextLink=function (){
    spanNext.innerHTML = "<a href='javascript:next();'>下一页</a>";
}
nextText=function (){
    spanNext.innerHTML = "下一页";
}
firstLink=function (){
    spanFirst.innerHTML = "<a href='javascript:first();'>首页</a>";
}
firstText=function (){
    spanFirst.innerHTML = "首页";
}
lastLink=function (){
    spanLast.innerHTML = "<a href='javascript:last();'>末页</a>";
}
lastText=function (){
    spanLast.innerHTML = "末页";
}

//隐藏
hide=function (){
    for ( var i = pageSize; i<numberRowsInTable; i++ ){
        theUL.getElementsByTagName("tr")[i].style.display = 'none';
    }
    totalPage.innerHTML = pageCount();
    pageNum.innerHTML = '1';
    nextLink();
    lastLink();
}
